<template>
  <view class="content">
    <view class="complete-box">
      <view class="complete-img">
        <image src="../../static/pay.png" mode />
      </view>
      <view class="complete-tleie">
        <view class="complete-h4">已完成当前菜品定制</view>
        <view class="complete-p">
          共完成定制
          <text>{{num}}</text>道菜
        </view>
      </view>
      <view class="complete-jianyi">
        <textarea v-model="remark" placeholder="如您对本菜品有具体的做法要求及建议，请在此添加备注。" @input="change" />
      </view>
      <view class="complete-an">
        <button type="default" class="jx-btn" @click="jxdc(1)">继续点餐</button>
        <button type="default" class="wanc-btn" @click="jxdc(2)">完成点餐</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      num: 0,
      remark: ""
    };
  },
  onLoad() {},
  onShow() {
    this.getGWC();
  },
  methods: {
    getGWC() {
      //获取购物车列表
      var than = this;
      uni.request({
        url: getApp().globalData.http + "/shoppingCart/getList",
        data: {},
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          console.log(res.data.data);
          if (res.data.code == 1) {
            if (getApp().globalData.dingzhicai.detail.goods.length != 0) {
              than.num = res.data.data.length + 1;
            } else {
              than.num = res.data.data.length;
            }
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },
    change() {
      console.log(this.remark);

      getApp().globalData.dingzhicai.remark = this.remark;
    },

    //继续点餐
    jxdc(num) {
      if (
        (getApp().globalData.dingzhicai.detail.goods == 0) &
        (this.num == 0)
      ) {
        wx.reLaunch({
          url: "/pages/my/custom-list"
        });
      } else {
        if (getApp().globalData.dingzhicai.detail.goods == 0) {
          if (num == 1) {
            wx.reLaunch({
              url: "/pages/my/custom-list"
            });
          } else {
            wx.navigateTo({
              url: "/pages/my/distribution"
            });
          }
        } else {
          uni.request({
            url: getApp().globalData.http + "/shoppingCart/put",
            data: {
              ...getApp().globalData.dingzhicai
            },
            method: "post",
            header: {
              token: uni.getStorageSync("token")
            },
            success: res => {
              if (res.data.code == 1) {
                getApp().globalData.dingzhicai = {
                  type: 2,
                  goods_id: 0,
                  num: 1,
                  remark: "",
                  detail: {
                    goods: [], //主菜
                    cooking: [], //烹饪方式
                    collocation: [], //配菜
                    seasoning: [] //调料
                  }
                };
                if (num == 1) {
                  wx.reLaunch({
                    url: "/pages/my/custom-list"
                  });
                } else {
                  wx.navigateTo({
                    url: "/pages/my/distribution"
                  });
                }
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: "none",
                  duration: 2000
                });
              }
            }
          });
        }
      }
    }
  }
};
</script>
